/**
 * 文件：GameLikeDAOImpl.java
 * 作者：朱娜
 * 完成度：已完成
 * 最后修改日期：2023-7-4 19:16
 */

//修改完成
package com.gomokult.dao.impl;

import com.gomokult.dao.GameLikeDAO;

import java.sql.ResultSet;
import java.sql.SQLException;

import static com.gomokult.utility.JDBCUtil.con;
import static com.gomokult.utility.JDBCUtil.pstat;
//修改结束
public class GameLikeDAOImpl implements GameLikeDAO {
	private GameLikeDAOImpl() {}
	private static final GameLikeDAOImpl singleton = new GameLikeDAOImpl();
	public static GameLikeDAOImpl getSingleton() { return singleton; }


	@Override
	public boolean selectLikeRecord(int userID, int gameID) {//正确
		try {
			String sql = "select * from gameLike where user = ? and game = ?";
			pstat = con.prepareStatement(sql);
			pstat.setInt(1,userID);
			pstat.setInt(2,gameID);
			ResultSet rs = pstat.executeQuery();
			return rs.next();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public int selectLikeRecord(int gameID) {
		try {
			String sql = "select * from gameLike where game = ?";
			pstat = con.prepareStatement(sql);
			pstat.setInt(1,gameID);
			ResultSet rs = pstat.executeQuery();
			int cnt=0;
			while(rs.next()){
				cnt++;
			}
			return cnt;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public void insertLikeRecord(int userID, int gameID) {//正确
		try {
			String sql = "insert into gameLike(game,user) values (?,?)";
			pstat = con.prepareStatement(sql);
			pstat.setInt(1,gameID);
			pstat.setInt(2,userID);
			pstat.executeUpdate();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}


	@Override
	public void deleteLikeRecord(int userID, int gameID) {//正确
		try {
			String sql = "delete from gameLike where game = ? and user = ?";
			pstat = con.prepareStatement(sql);
			pstat.setInt(1,gameID);
			pstat.setInt(2,userID);
			pstat.executeUpdate();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
}
